'use server';
import Title from 'antd/es/typography/Title';
import { Divider, Flex } from 'antd';
import Link from 'next/link';
import './index.css';
import BankList from '@/components/BankList';
import QuestionList from '@/components/QuestionList';
import { listBankUsingPost } from '@/api/BankApi';
import { listQuestionsVoUsingPost } from '@/api/QuestionApi';

/**
 * 主页
 * @constructor
 */
export default async function HomePage() {
  let bankList = [];
  let questionList = [];
  try {
    const res = await listBankUsingPost({
      pageSize: 12,
      sortField: 'createTime',
      sortOrder: 'descend',
    });
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-expect-error
    bankList = res.data.records ?? [];
  } catch (e) {
    console.error('Failed to fetch bank list:', e);
  }

  try {
    const res = await listQuestionsVoUsingPost({
      pageSize: 12,
      sortField: 'createTime',
      sortOrder: 'descend',
    });
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-expect-error
    questionList = res.data.records ?? [];
  } catch (e) {}

  return (
    <div id="homePage" className="max-width-content">
      <Flex justify="space-between" align="center">
        <Title level={3}>最新题库</Title>
        <Link href={'/banks'}>查看更多</Link>
      </Flex>
      <BankList bankList={bankList} />
      <Divider />
      <Flex justify="space-between" align="center">
        <Title level={3}>最新题目</Title>
        <Link href={'/questions'}>查看更多</Link>
      </Flex>
      <QuestionList questionList={questionList} />
    </div>
  );
}
